<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>心灵笔触</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/urlutis.js"></script>
    <script src="js/logout.js"></script>
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
            border-radius: 10px;
            transition: background-color 0.2s ease; 
        }
        .blog-pagnation-item:hover{ 
            background: rgb(106, 78, 220);
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo3.jpg" alt="">
        <span class="title">心灵笔触</span>
        &nbsp;
        <a href="recommend.html">热榜推荐</a>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="search.html">搜索</a>
        <a href="readerinfo.html">修改</a>
        <a href="javascript:logout()">退出</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id="artListDiv">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->
                
            </div>
            
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="doFirst()">首页</button> 
                <button class="blog-pagnation-item" onclick="doBefore()">上一页</button>  
                <button class="blog-pagnation-item" onclick="doNext()">下一页</button>
                <button class="blog-pagnation-item" onclick="doLast()">末页</button>
                &nbsp;&nbsp;&nbsp;&nbsp;
                第 <span id="index"></span> 页,
                共  <span id="psize"></span>  页
            </div>
        </div>
    </div>
    <script>
        var psize = 3;  // 每页显示的条数
        var pindex = 1; // 页面
        var totalpage = 1; // 总共有多少页
        // 初始化数据
        function init() {
            // 1. 得到 url 中的分页参数,处理分页参数
            psize = getParamValue("psize");
            if(psize == null) {
                psize = 3;  // 默认显示条数
            }
            pindex = getParamValue("pindex");
            if(pindex == null) {
                pindex = 1;  // 页码
            }
            jQuery("#index").html(pindex);
            // 2. 请求后端接口
            jQuery.ajax({
                url:"/art/getlistbypage",
                type:"GET",
                data:{
                    "psize":psize,
                    "pindex":pindex
                },
                success:function(res) {
                    // 3. 将结果展示给用户
                    if(res.code == 200 && res.data != null) {
                        // 拼接
                        var createHtml = "";
                        if(res.data.list != null && res.data.list.length > 0) {
                            // 有文章
                            totalpage = res.data.size;
                            jQuery("#psize").html(totalpage);
                            var artlist = res.data.list;
                            for(var i = 0; i < artlist.length; i++){
                                var art = artlist[i];
                                createHtml += '<div class="blog" >';
                                createHtml += '<div class="title">'+art.title+'</div>';
                                createHtml += '<div class="date">'+art.createtime+'</div>';
                                createHtml += '<div class="desc">'+art.content+'</div>';
                                createHtml += '<a href="blog_content.html?aid='+art.id
                                +'" class="detail">查看全文 &gt;&gt;</a>';
                                createHtml += '<a href="discuss_reader.html?aid='+art.id
                                +'" class="detail" >评论 &gt;&gt;</a>';
                                createHtml += '</div>';
                            }
                            jQuery("#artListDiv").html(createHtml);
                        }else {
                            // 暂无文章
                            createHtml += '<h3 style="margin-top:20px;margin-left:20px;">暂无文章！</h3>';
                        }
                    }else {
                        alert("抱歉：查询失败！" + res.msg);
                    }
                }
            });
            
        }
        init();


        // 点击首页
        function doFirst(){
            // 1.判断是否在首页
            if(pindex <= 1) {
                alert("已至首页");
                return false;
            }
            // 2.跳到首页
            location.href = "reader_list.html?pindex=1";
        }

        // 点击末页
        function doLast() {
            // 1.判断是否在末页
            if(pindex >= totalpage){
                alert("已至末页");
                return false;
            }
            // 2.跳到末页
            location.href = "reader_list.html?pindex=" + totalpage;
        }

        // 点击 上一页
        function doBefore() {
            // 1.判断是否在首页
            if(pindex <= 1) {
                alert("前面没有了哦！");
                return false;
            }
            // 2.跳转
            location.href = "reader_list.html?pindex=" + (parseInt(pindex) - 1);
        }

        // 点击 下一页
        function doNext() {
            // 1.判断是否在末页
            if(pindex >= totalpage) {
                alert("后面没有了，期待你的发挥！");
                return false;
            }
            // 2.跳转
            location.href = "reader_list.html?pindex=" + (parseInt(pindex) + 1);
        }

    </script>
</body>
</html>